<script setup>
import * as XLSX from 'xlsx';
import { specialRoomListCommon } from '@/components/roomConstant/common.js';

const props = defineProps({
  dataSource: {
    type: Array,
    default: () => []
  }
});

const fieldMapping = {
  'name': '房间号/名称',
  'area': '面积(㎡)',
  'roomUseUnit': '使用单位',
  'roomUsePeople': '使用人',
};

const dataSourceHandle = computed(() => {
  return props.dataSource.map(item => {
    const temp = {
      ...item,
      roomUseUnit: specialRoomListCommon.roomUseUnit[item.roomUseUnit]?.name,
    };
    const converted = {};
    Object.keys(item).forEach(key => {
      const chineseKey = fieldMapping[key] || key;
      converted[chineseKey] = temp[key];
    });
    return converted;
  });
});

const exportToExcel = () => {
  const worksheet = XLSX.utils.json_to_sheet(dataSourceHandle.value);
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
  XLSX.writeFile(workbook, '房间列表' + Date.now() + '.xlsx');
};
</script>

<template>
  <div class="export-container">
    <a-button type="primary" @click="exportToExcel">导出列表</a-button>
  </div>
</template>

<style scoped lang="scss">

</style>
